﻿using System;
using System.Collections.Generic;
using System.Text;

public class AddsOfPolynomials
{
    public static decimal[] a;
    public static decimal[] b;

    public static StringBuilder description = new StringBuilder();
    public static bool started = false;
    public static decimal koefficient;

    public static void Main()
    {
        DataInput();
        PrintResult(Adds(a, b), "Plus");
    }

    public static void DataInput()
    {
        List<decimal> polynom = new List<decimal>();
        int power;
        description.Clear();
        started = false;

        for (int i = 1; i <= 2; i++)
        {
            started = false;
            polynom.Clear();

            description.Append(string.Format("\n----- {0} polynomial -----\n\n", i == 1 ? "First" : "Second"));

            Console.Clear();
            power = Common.InputNonNegativeInteger(description.ToString() + "Power = ");

            for (int j = power; j >= 0; j--)
            {
                Console.Clear();
                Console.Write(description.ToString());

                do
                {
                    Console.Clear();
                    Console.Write(description.ToString());

                    if ((j < power) && started)
                        Console.Write("  +  ");

                    if (0 < j)
                        Console.Write("x^" + j + " * ");

                    if (decimal.TryParse(Console.ReadLine(), out koefficient))
                        break;
                } while (true);

                polynom.Add(koefficient);
                AddIt(j);

            }

            description.Append("\n\n");
            if (i == 1)
            {
                a = new decimal[polynom.Count];
                for (int j = 0; j < polynom.Count; j++)
                    a[j] = polynom[polynom.Count - 1 - j];
            }
            else
            {
                b = new decimal[polynom.Count];
                for (int j = 0; j < polynom.Count; j++)
                    b[j] = polynom[polynom.Count - 1 - j];
            }
        }

        Console.Clear();
        Console.WriteLine("{0}", description.ToString());
    }

    public static void AddIt(int j)
    {
        if (koefficient != 0)
        {
            if (koefficient < 0)
            {
                koefficient = -koefficient;
                if (started)
                    description.Append(" - ");
                else
                    description.Append("- ");
            }
            else if (started)
                description.Append(" + ");

            started = true;

            if (koefficient != 1)
            {
                description.Append(koefficient);
                if (0 < j)
                    description.Append("*");
            }
            else if (j == 0)
                description.Append(koefficient);

            if (0 < j)
                description.Append("x^" + j);
        }
    }

    public static decimal[] Adds(decimal[] a, decimal[] b)
    {
        int maxLength = Common.GetMax(a.Length, b.Length);
        decimal[] aPlusB = new decimal[maxLength];

        for (int i = 0; i < maxLength; i++)
            aPlusB[i] = (i < a.Length ? a[i] : 0) + (i < b.Length ? b[i] : 0);

        return aPlusB;
    }

    public static void PrintResult(decimal[] array, string operation)
    {
        description.Clear();
        started = false;

        description.Append("\n-----first " + operation + " second -----\n\n");
        for (int j = array.Length - 1; j >= 0; j--)
        {
            koefficient = array[j];
            AddIt(j);
        }

        Console.WriteLine(description.ToString() + "\n\n");
    }
}